<template>
  <div :class="{'hidden':hidden}" class="pagination-container">
    <el-pagination
      :background="background"
      :current-page.sync="currentPage"
      :page-size.sync="pageSize"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      prev-text="<上一页"
      next-text="下一页>"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange">
    </el-pagination>
    <!-- <span class="handleSure cursor" @click="handleSure">确定</span> -->
  </div>
</template>

<script>
import { scrollTo } from '@/utils/scrollTo'

export default {
  name: 'Pagination',
  props: {
    total: {
      required: true,
      type: Number
    },
    page: {
      type: Number,
      default: 1
    },
    limit: {
      type: Number,
      default: 20
    },
    pageSizes: {
      type: Array,
      default() {
        return [12,24,48,60]
      }
    },
    layout: {
      type: String,
      default: ' sizes, prev, pager, next, total, jumper'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      myCurrentPage: null,
    }
  },
  computed: {
    currentPage: {
      get() {
        return this.page
      },
      set(val) {
        this.$emit('update:page', val)
      }
    },
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      this.$emit('pagination', { page: this.currentPage, limit: val })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleCurrentChange(val) {
      // this.myCurrentPage = val;
      this.$emit('pagination', { page: this.currentPage, limit: this.pageSize })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    handleSure() {
      this.$emit('pagination', { page: this.myCurrentPage, limit: this.pageSize })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    }
  }
}
</script>

<style lang="scss">
.pagination-container {
  background: #fff;
  padding: 32px 16px;
  .el-pagination.is-background .btn-next, .el-pagination.is-background .btn-prev{
    width: 70px !important;
  }
  .el-pagination__total{
    margin-left: 20px;
  }
  .el-pagination__jump{
    margin-left: 10px;
  }
  .handleSure{
    display: inline-block;
    margin-top: 4px;
    margin: 0 5px;
    margin-left: 10px;
    background-color: #f4f4f5;
    color: #606266;
    min-width: 30px;
    border-radius: 2px;
    width: 60px;
    line-height: 30px;
    text-align: center;
    border: 1px solid #ddd;
    vertical-align: bottom;
  }
}
.pagination-container.hidden {
  display: none;
}
.el-pagination{
  white-space: normal;
  display: inline-block;
}


</style>
